<template>
  <div id="app">
    <button @click="addNum">添加数字</button>
    <button @click="removeNum">删除数字</button>
    <button @click="shuffleNum">数字洗牌</button>
    <transition-group tag="p" name="why">
      <span v-for="item in numbers" :key="item" class="item">
        {{ item }}
      </span>
    </transition-group>
  </div>
</template>

<script>
import _ from "lodash";
export default {
  name: "App",
  data() {
    return {
      numbers: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
      numCounter: 10,
    };
  },
  created() {},
  methods: {
    addNum() {
      this.numbers.splice(this.randomIndex(), 0, this.numCounter);
      this.numCounter++;
    },
    removeNum() {
      this.numbers.splice(this.randomIndex(), 1);
      this.numCounter--;
    },
    shuffleNum() {
      this.numbers = _.shuffle(this.numbers);
    },
    randomIndex() {
      return Math.floor(Math.random() * this.numbers.length);
    },
  },
};
</script>

<style lang="less">
.item {
  margin-right: 20px;
  font-size: 30px;
  /* 因为行内元素宽高固定的，如果不变块下面的transform变化无效 */
  display: inline-block;
}
.why-enter-from,
.why-leave-to {
  opacity: 0;
  transform: translateY(30px);
}
.why-enter-active,
.why-leave-active {
  transition: all 1s ease;
}
/* 因为在离开的过程中，item会占据位置，导致动画过不去不能生效，所以要加定位让他离开的时候宽度没掉 */
.why-leave-active {
  position: absolute;
}
.why-move {
  transition: transform 1s ease;
}
</style>
